package com.zdkj.subway.check.util;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * 下载工具
 */
public class DownLoadUtil {
    public static void downloadUtil(HttpServletResponse response, InputStream fileInputStream, String strName){
        ServletOutputStream out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-tar");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(strName, "utf-8"));
            out = response.getOutputStream();
            int b = 0;
            byte[] buffer = new byte[1024];
            while ((b = fileInputStream.read(buffer)) != -1) {
                out.write(buffer, 0, b);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                out.flush();
                out.close();
                fileInputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
